﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Linq;
using OctoOS;
using OctoOS.Services;
using OctoOS.UI;

public partial class __flag : OOSPage
{
	class PathInfo
	{
		[PathInfoMember(0)]
		internal long adID;
	}

	PathInfo _pathInfo;
    ClassifiedAd _ad = null;

	protected void Page_Init(object sender, EventArgs e)
	{
		_pathInfo = EnablePathInfo<PathInfo>();
        Helper.Categories.UpdateCategoryCache();
	}

   protected void Page_Load(object sender, EventArgs e)
   {
       PageTitle = "Abuse Ad";
       
       _ad = DataSource.ClassifiedAds_GetByID(
            OOSRequest.Current.ResourceID,
            0,
            _pathInfo.adID);
       if (_ad != null)
       {
           SetBreadcrumbs();
           Author.Text = _ad.UserID == 0 ? "Anonymous" : _ad.UserID.ToString();
           Category.Text = GetAdCategory();
           Title.Text = _ad.Title;
           AdText.Text = _ad.AdText;
           PostTime.Text = _ad.PostTime.ToShortDateString();
           Latitude.Text = _ad.Latitude == 0 ? "not defined" : _ad.Latitude.ToString();
           Longitude.Text = _ad.Longitude == 0 ? "not defined" :_ad.Longitude.ToString();
           ExpiryTime.Text = _ad.ExpiryTime.ToShortDateString();
           PriceType.Text = _ad.PriceType == 0 ? "Free" :
               _ad.PriceType == 0 ? "Please contact" : "Money";
           Price.Text = _ad.Price.ToString("F2");
           Currency.Text = _ad.CurrencyCode;
           AdType.Text = _ad.Searching == false ? "Offer" : "Need";
           Phone.Text = _ad.ContactPhone;
           Mail.Text = _ad.ContactEMail;
           Url.Text = _ad.ContactUrl;
           foreach (var attribute in _ad.Attributes.OrderByDescending(f => f.SortOrder))
           {
               // if there is choosen value
               if (!attribute.IsCustom)
               {
                   if (attribute.AttributeValues.Where(f => f.Selected == true).Count() > 0)
                   {
                       FormRow row = new FormRow();
                       row.Label = attribute.Title;
                       var value = attribute.AttributeValues.FirstOrDefault(f => f.Selected == true);
                       if (value != null)
                       {
                           OctoOS.UI.Literal lit = new OctoOS.UI.Literal(value.AttributeValueName);
                           row.Controls.Add(lit);
                           FormView.Controls.AddAt(FormView.Controls.IndexOf(FormView.FindControl("LongitudeRow")) + 1, row);
                       }
                       else
                       {
                           break;
                       }

                   }
               }
               else
               {
                   if (attribute.AttributeValues.Count() > 0)
                   {
                       FormRow row = new FormRow();
                       row.Label = attribute.Title;
                       OctoOS.UI.Literal lit = new OctoOS.UI.Literal(attribute.AttributeValues[0].AttributeValueName);
                       row.Controls.Add(lit);
                       FormView.Controls.AddAt(FormView.Controls.IndexOf(FormView.FindControl("LongitudeRow")) + 1, row);
                   }
               }
           }

       }
       else
       {
           throw new HttpException(404, "This page is not found");
       }
   }

   private void SetBreadcrumbs()
   {
       AddBreadcrumb(string.Empty, "/");
       Category current = Helper.Categories.GetCategory(_ad.CategoryID);
       Category parent = Helper.Categories.GetParentCategory(_ad.CategoryID);
       Category grandParent = Helper.Categories.GetGrandParentCategory(_ad.CategoryID);
       if (grandParent != null)
       {
           AddBreadcrumb(grandParent.Name, Urls.Classifieds.GetCategoryInfo(grandParent.ID));
       }
       if (parent != null)
       {
           AddBreadcrumb(parent.Name, Urls.Classifieds.GetCategoryInfo(parent.ID));
       }
       if (current != null)
       {
           AddBreadcrumb(current.Name, Urls.Classifieds.GetCategoryInfo(current.ID));
       }
       AddBreadcrumb(_ad.Title, Urls.Classifieds.GetInfoAd(_ad.ID));

   }

   private string GetAdCategory()
   {
       string result = "";

       Category currentCategory = Helper.Categories.GetCategory(_ad.CategoryID);
       Category parentCategory = Helper.Categories.GetParentCategory(_ad.CategoryID);
       Category grandParentCategory = Helper.Categories.GetGrandParentCategory(_ad.CategoryID);
       if (grandParentCategory != null)
       {
           result = grandParentCategory.Name + " - ";
       }
       if (parentCategory != null)
       {
           result += parentCategory.Name + " - ";
       }
       if (currentCategory != null)
       {
           result += currentCategory.Name;
       }
       return result;
   }

	protected void OnFormSubmit(object source, EventArgs e)
	{
		if(IsValid)
		{
			IEntityService srv = ServiceFactory.GetService<IEntityService>();
			using(srv as IDisposable)
			{
				srv.AddEntityAbuse(OOSRequest.Current.Token, _pathInfo.adID, message.TrimmedText);
			}

			Response.Redirect(Urls.Classifieds.GetInfoAd(_pathInfo.adID));
		}
	}
}